let hi = require('./util/HttpHelper');
//let names = require("./names.json");
function PostName(name,callback) {
    //name=encodeURIComponent(name);
    //console.log(name);
    let postData = {
        "actId": "20190515000104",
        "salesCode": "13242900031886",
        "seq": "1",
        "salesName": "王利萍",
        "pubType": "001",
        "channel": "PA",
        "feedBackContent": JSON.stringify({
            "content": {
                "type": "01",
                "1": name,
                "8": "",
                "25": "13423232343",
                "181": "130000",
                "182": "130600",
                "183": "130623"
            }, "actId": "20190515000104", "seq": "1", "salesCode": "13242900031886"
        }),
        "feedBackContentP": JSON.stringify({
            "content": {
                "1": name,
                "3": "",
                "181": "130000",
                "182": "130600",
                "183": "130623",
                "type": "02"
            }, "actId": "20190515000104", "seq": "1", "salesCode": "13242900031886"
        }),
        "openId": "oxv1Ot_tixXBHcntO-uMVI5CVg9A",
        "uniqueId": "801D4E75409464B2AA2158AB85C500446E26AEC8",
        "branchCode": "132429",
        "phoneCode": ""
    };
    hi.Ajax({
        url:'http://activity.e-chinalife.com/activitymobile/action/addCustBack.do',
        method:"POST",
        form:postData,
        headers:{
            "Referer":"http://activity.e-chinalife.com/activitymobileweb/activitydetailmobile/html/feedbackDetail.html?phone=15131295916&actId=20190515000104&salesmenName=%E7%8E%8B%E5%88%A9%E8%90%8D&opId=13242900031886&channel=PA&branchNo=132429&pubtype=001&deep=3&uniqueId=801D4E75409464B2AA2158AB85C500446E26AEC8&openId=oxv1Ot_tixXBHcntO-uMVI5CVg9A"
            ,"User-Agent":"Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Mobile Safari/537.36"
        }
    }).then(res=>{
        callback && callback(JSON.parse(res.body));
    }).catch(()=>{
        callback && callback(false);
    });
}
function SxNames(){
    let sX=0;
    let sY=0;
    let familyNames =[
        "赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈",
        "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
        "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏",
        "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章",
        "云", "苏", "潘", "葛", "奚", "范", "彭", "郎", "鲁", "韦",
        "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳",
        "酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺",
        "倪", "汤", "滕", "殷", "罗", "毕", "郝", "邬", "安", "常",
        "乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余",
        "元", "卜", "顾", "孟", "平", "黄", "和", "穆", "萧", "尹"
    ];
    let givenNames =[
        "子","璇","子璇", "淼", "国栋", "夫子", "瑞堂", "甜", "敏", "尚", "国贤", "贺祥", "晨涛",
        "昊","轩","昊轩", "易","易轩", "益辰", "益帆", "益冉", "瑾春", "瑾昆", "春齐", "杨", "文昊",
        "东","东","东东", "雄","雄霖", "浩晨", "熙涵", "溶溶", "冰枫", "欣欣", "宜豪", "欣慧", "建政",
        "美","欣","美欣", "淑","淑慧", "文轩", "文杰", "欣源", "忠林", "榕润", "欣汝", "慧嘉", "新建",
        "建","林","建林", "亦","亦菲", "林", "冰洁", "佳欣", "涵涵", "禹辰", "淳美", "泽惠", "伟洋",
        "涵","越","涵越", "润","润丽", "翔", "淑华", "晶莹", "凌晶", "苒溪", "雨涵", "嘉怡", "佳毅",
        "子","辰","子辰", "佳","佳琪", "紫轩", "瑞辰", "昕蕊", "萌", "明远", "欣宜", "泽远", "欣怡",
        "佳","怡","佳怡", "佳","佳惠", "晨茜","茜","晨","晨璐", "运昊", "汝鑫", "淑君", "晶滢", "润莎", "榕汕",
        "佳","钰","佳钰", "佳","佳玉", "晓庆","庆","一","一鸣", "语晨", "添池", "添昊", "雨泽", "雅晗", "雅涵",
        "清","妍","清妍", "诗","诗悦", "嘉乐","乐","晨","晨涵", "天赫", "玥傲", "佳昊", "天昊", "萌萌", "若萌",

    ];
    function getName(){
        let name=familyNames[sX]+givenNames[sY];
        sY++;
        if(sY>=givenNames.length){
            sY=0;
            sX++;
        }
        if(sX>=familyNames.length){
            console.log('结束');
            return null;
        }
        return name;
    }
    console.log("开始生成！！！");
//生成
    let list=[];
    while(true){
        let name=getName();
        if(name===null){
            break;
        }else{
            list.push(name);
        }
    }
    console.log("生成结束！！！");
    console.log(list.length);
    return list;
}

let names=SxNames();
let list=[];
let startIndex=0;
function send(index){
    if(!names[index]){
        console.log('任务执行完毕');
        let keys=[];
        list.forEach(x=>!keys.includes(x.code) && keys.push(x.code));
        //key获取完毕
        keys.forEach((code)=>{
            console.log(code+":"+list.filter(x=>x.code===code).length);
        });
        console.log('程序结束运行');
        return;
    }
    let namesKey=names[index];
    try{
        PostName(namesKey,(json)=>{
            if(json===false){
                send(index);
                return;
            }
            list.push(json);
            console.log(json.message+"----"+namesKey);
            if(list.length>=names.length){
                console.log('任务执行完毕');
                let keys=[];
                list.forEach(x=>!keys.includes(x.code) && keys.push(x.code));
                //key获取完毕
                keys.forEach((code)=>{
                    console.log(code+":"+list.filter(x=>x.code===code).length);
                });
                console.log('程序结束运行');
            }else{
                send(++index);
            }
        });
    }catch (e) {
        send(index);
    }
}
//names.forEach((x,i)=>x=='莘文斌' && (startIndex=i));
send(startIndex);